<?php
$page = 'admin';
include '../database/connect.php';
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO courses SET course='$course', added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}


if (isset($_GET['update'])) {
    session_start();
    extract($_POST);
    $tid = $_GET['id'];
    $query = "UPDATE courses SET course='$course' WHERE id='$tid' ";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?");
}
?>
<?php require_once 'header.php'; ?>


<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;Courses
</p>

<div id="edit_result"></div>
<div id="delete_result" style="display:none">
    <p class="error">course deleted!</p>
</div>
<script type="text/javascript">
    initDialog("#edit_result","Edit course");
    initDialog("#delete_result","course deleted");
            
    

    jQuery(document).ready(function(){
        jQuery("#course_form").validationEngine();
    });

    function editcourse(tid){
        $("#edit_result").hide();
        $("#loading").show();
        //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
        $("#edit_result").load("../actions/edit_courses.php?id="+tid,function() {
            openDialog('#edit_result');
            $("#edit_result").show();
            $("#loading").hide();
        });
    }
            
  
    
    function deletecourse(tid){
        if(confirm("Are you sure you want to delete?")){
            $("#loading").show();
            //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
            $("#edit_result").load("../actions/delete_courses.php?id="+tid,function() {
                openDialog('#delete_result');
                $("#loading").hide();
            });
            window.location="?";
        }
    }
</script>
<img src="../images/loading.gif" alt="Loading.." style="display: none;" id="loading"/>
<form action="?save=1" method="POST" id="course_form">
    <table class="table">
        <tr>
            <td>Course Name</td>
            <td><input type="text" class="validate[required] wide" name="course" id='course'/></td>
            <td><input type="submit"/></td>
        </tr>
    </table>
</form>

<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM courses order by course asc";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) == 0) {
    echo "No course in the database";
} else {
?>

    <table class="table" style="text-align: left">
        <tr>
            <th >
                Course name
            </th>
            <th >
                Quiz
            </th>
            <th >
                Edit
            </th>

            <th >Delete
            </th>
        </tr>
    <?php
    while ($row = mysql_fetch_assoc($result)) {
        //var_dump($row);
        extract($row);
    ?>
        <tr>
            <td >
            <?php
            echo "<a href='lessons.php?course_id=$id'>$course</a>";
            ?>
        </td>
        <td >
            <?php
            $queryx = "SELECT count(*) as count1 FROM quizes where course_id='$id'";
            $resultx = mysql_query($queryx) or die("Error executing '$queryx':" . mysql_error());
            $row = mysql_fetch_assoc($resultx);
            $count = $row['count1'];
            ?>
            <a href="quizes.php?course_id=<?php echo $id ?>">Quizes (<?php echo $count; ?>)</a>
        </td>
        <td >
            <a href="javascript:editcourse(<?php echo $id ?>);"><img src="../images//edit.png" alt="Edit" title="Edit"/></a>
        </td><td>
            <a href="javascript:deletecourse(<?php echo $id ?>);"><img src="../images//delete.png" alt="Delete" title="Delete"/></a>
        </td>
    </tr>
    <?php
        }
    }
    ?>
</table>


<?php require_once('footer.php'); ?>